import jdk.swing.interop.SwingInterOpUtils;

import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;

    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) return ;
        System.out.print(root.val + " ");

        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) return ;
        inOrder(root.left);
        System.out.print(root.val + " ");

        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) return ;
        postOrder(root.left);
        postOrder(root.right);

        System.out.print(root.val + " ");
    }

    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */

    public int size(TreeNode root) {
        if(root == null) return -1;
        nodeSize++;
        size(root.left);
        size(root.right);
        return nodeSize;
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    public int size2(TreeNode root) {
        if(root == null) return 0;

        return size2(root.left) + size2(root.right) + 1;

    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;


    public int getLeafNodeCount1(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);

        return leafSize;

    }

    /*
     获取叶子节点的个数：子问题
     */
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left) +
                getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    public int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) return 0;
        if(k == 1){
            return 1;
        }

        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    public int getHight(TreeNode root) {
        if(root == null) return 0;

        return Math.max(getHight(root.left),getHight(root.right)) + 1;
    }

    public int getHight2(TreeNode root) {
        if(root == null) return 0;
        int leftHight = getHight(root.left);
        int rightHight = getHight(root.right);

        return leftHight > rightHight ? leftHight + 1 : rightHight + 1;
    }


    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val) {
        if(root == null) return null;
        if(root.val == val){
            System.out.println("存在该元素");
            return root;
        }
        find(root.left,val);
        find(root.right,val);
        return null;
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if(root == null) return ;
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        while(!qu.isEmpty()){
            TreeNode cur = qu.poll();
            System.out.print(cur.val);
            if(cur.left != null){
                qu.offer(cur.left);
            }
            if(cur.right != null){
                qu.offer(cur.right);
            }
        }
    }

//    public List<List<Integer>> levelOrder2(TreeNode root) {
//        List<List<Integer>> ret = new ArrayList<>();
//        if(root == null) return ret;
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//        while(!queue.isEmpty()){
//            List<Integer> tmpList = new LinkedList<>();
//            int size = queue.size();
//            while(size != 0){
//                TreeNode cur = queue.poll();
//                size--;
//                tmpList.add(cur.val);
//                if(cur.left != null){
//                    queue.offer(cur.left);
//                }
//                if(cur.right != null){
//                    queue.offer(cur.right);
//                }
//            }
//            ret.add(tmpList);
//        }
//        return ret;
//    }



    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        while(!qu.isEmpty()){
            TreeNode cur = qu.poll();
            if(cur != null){
                qu.offer(cur.left);
                qu.offer(cur.right);
            }else{
                break;
            }
        }
        while(!qu.isEmpty()){
            TreeNode cur = qu.poll();
            if(cur != null){
                return false;
            }
        }
        return true;
    }

    //二叉树遍历
//    class TreeNode {
//        public char val;
//        public TreeNode left;
//        public TreeNode right;
//
//        public TreeNode(char val) {
//            this.val = val;
//        }
//    }
//
//    // 注意类名必须为 Main, 不要有任何 package xxx 信息
//    public class Main {
//
//        public static int i = 0;
//        public static void main(String[] args) {
//            Scanner in = new Scanner(System.in);
//            // 注意 hasNext 和 hasNextLine 的区别
//            while (in.hasNextLine()) { // 注意 while 处理多个 case
//                String str = in.nextLine();
//                TreeNode root = createTree(str);
//                inOrder(root);
//            }
//        }
//
//        public static TreeNode createTree(String str) {
//            TreeNode root = null;
//            if(str.charAt(i) != '#'){
//                root = new TreeNode(str.charAt(i));
//                i++;
//                root.left = createTree(str);
//                root.right = createTree(str);
//            }else{
//                i++;
//            }
//            return root;
//        }
//
//        public static void inOrder(TreeNode root) {
//            if (root == null) return ;
//
//            inOrder(root.left);
//            System.out.print(root.val + " ");
//            inOrder(root.right);
//        }
//    }

    //二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return null;
        if(root.val == p.val || root.val == q.val){
            return root;
        }
        TreeNode LeftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        if(LeftTree != null && rightTree != null){
            return root;
        }
        if(LeftTree != null && rightTree == null){
            return LeftTree;
        }
        if(LeftTree == null && rightTree != null){
            return rightTree;
        }
        return null;
    }


    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);
        int lenp = stackP.size();
        int lenq = stackQ.size();
        if(lenp < lenq){
            int len = lenq - lenp;
            while(len != 0){
                stackQ.pop();
                len--;
            }
        }else{
            int len = lenp - lenq;
            while(len != 0){
                stackP.pop();
                len--;
            }
        }
        while(!stackP.isEmpty() && !stackQ.isEmpty()){
            TreeNode valp = stackP.pop();
            TreeNode valq = stackQ.pop();
            if(valp == valq){
                return valp;
            }
        }
        return null;
    }

    public static boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if(root == null) return false;
        stack.push(root);
        if(root == node){
            return true;
        }
        boolean flg = getPath(root.left,node,stack);
        if(flg){
            return true;
        }
        flg = getPath(root.right,node,stack);
        if(flg){
            return true;
        }
        stack.pop();
        return false;
    }


//    //前序与中序遍历构建二叉树
//    public int preIndex = 0;
//
//    public TreeNode buildTree(int[] preorder, int[] inorder) {
//        return buildTreeChild(preorder,inorder,0,inorder.length - 1);
//    }
//
//    public TreeNode buildTreeChild(int[] preorder, int[] inorder,int inbegin,int inend) {
//        if(inbegin > inend){
//            return null;
//        }
//        TreeNode root = new TreeNode(preorder[preIndex]);
//        int rootIndex = findVal(inorder,inbegin,inend,preorder[preIndex]);
//        preIndex++;
//        root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex - 1);
//        root.right = buildTreeChild(preorder,inorder,rootIndex + 1,inend);
//        return root;
//    }
//
//    private int findVal(int[] inorder,int inbegin,int inend,int key){
//        for(int i = inbegin; i <= inend;i++){
//            if(inorder[i] == key){
//                return i;
//            }
//        }
//        return -1;
//    }


}
